package org.jix.weblog.module.common.utils;

import lombok.Data;
import org.jix.weblog.module.common.exception.BaseExceptionInterface;
import org.jix.weblog.module.common.exception.BizException;

/**
 * @version 1.0
 * @Author Jix
 * @Date 2024/7/16 10:06
 * @注释
 */

@Data
public class Response<T> {
    //是否成功，默认成功
    private boolean success = true;
    //响应消息
    private String errorMsg;
    //异常码
    private String errorCode;
    //响应数据
    private T data;

    //=================成功响应===================//
    public static <T> Response<T> success(){
        Response<T> response = new Response<>();
        return response;
    }
    public static <T> Response<T> success(T data){
        Response<T> response = new Response<>();
        response.setData(data);
        return response;
    }

    //=================响应失败==================//
    public static <T> Response<T> fail(){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        return response;
    }
    public static <T> Response<T> fail(String errorMsg){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setErrorMsg(errorMsg);
        return response;
    }
    public static <T> Response<T> fail(String errorCode, String errorMsg){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setErrorMsg(errorMsg);
        response.setErrorCode(errorCode);
        return response;
    }
    public static <T> Response<T> fail(BizException bizException){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setErrorCode(bizException.getErrorCode());
        response.setErrorMsg(bizException.getErrorMsg());
        return response;
    }
    public static <T> Response<T> fail(BaseExceptionInterface baseExceptionInterface) {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setErrorCode(baseExceptionInterface.getErrorCode());
        response.setErrorMsg(baseExceptionInterface.getErrorMessage());
        return response;
    }
}
